package com.iwith.dongmoon.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.gsonlist.BoardListItem;
import com.iwith.dongmoon.gsonlist.MemberList;
import com.iwith.dongmoon.gsonlist.MemberListItem;
import com.iwith.dongmoon.listadpater.ThreeTextBoardListAdapter;
import com.iwith.dongmoon.listadpater.ThreeTextMemberListAdapter;
import com.iwith.dongmoon.listadpater.TwoTextListAdapter;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class Tab_MainActivity extends Activity {
	private RequestGson requestGson = new RequestGson();

	private TabHost mTabHost;

	private ListView newmemberListView;
	private ListView occasionListView;
	private ListView meetingListView;
	private ListView advertisementListView;
	private ListView noticeListView;
	
	private Context mContext;

	private final String [] TAB_ID = { ApiCode.OCCASIONS, ApiCode.MEETINGS, ApiCode.ADVERTISEMENTS };
	private final String [] TAB_NAME = { "경조사", "모임", "업체홍보" };
	private final Integer [] TAB_RESOURCE = { R.id.tabmain_occasionlistview, R.id.tabmain_meetinglistview, R.id.tabmain_advertisementlistview };
	
	
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_main);
		// TODO Auto-generated method stub
		
		mTabHost = (TabHost) findViewById(R.id.tabmain_tabhost);
		mTabHost.setup();

		for(int i=0; i<TAB_ID.length; i++){
			mTabHost.addTab(mTabHost.newTabSpec(TAB_ID[i]).setIndicator(TAB_NAME[i]).setContent(TAB_RESOURCE[i]));
			mTabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 70;
		}
		mTabHost.setCurrentTab(0);
		
		Initialize();
	}

	@Override
	public void onResume() {
		super.onResume();
		Initialize();
	}

	private void Initialize() {
		mContext=this;
		
		newmemberListView = (ListView) findViewById(R.id.tabmain_newmemberlistview);
		occasionListView = (ListView) findViewById(R.id.tabmain_occasionlistview);
		meetingListView = (ListView) findViewById(R.id.tabmain_meetinglistview);
		advertisementListView = (ListView) findViewById(R.id.tabmain_advertisementlistview);
		noticeListView = (ListView) findViewById(R.id.tabmain_noticelistview);

		setMemberListData(this, ApiCode.RECENTUSER, newmemberListView);
		
		if(mTabHost.getCurrentTabTag().equals(TAB_ID[0])==true){
			setBoardListData(mContext, ApiCode.OCCASIONS, occasionListView);
		} else if(mTabHost.getCurrentTabTag().equals(TAB_ID[1])==true){
			setBoardListData(mContext, ApiCode.MEETINGS, meetingListView);
		} else if(mTabHost.getCurrentTabTag().equals(TAB_ID[2])==true){
			setBoardListData(mContext, ApiCode.ADVERTISEMENTS, advertisementListView);
		}
		
		
		mTabHost.setOnTabChangedListener(new OnTabChangeListener(){

			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				if(tabId.equals(TAB_ID[0])==true){
					setBoardListData(mContext, ApiCode.OCCASIONS, occasionListView);
				} else if(tabId.equals(TAB_ID[1])==true){
					setBoardListData(mContext, ApiCode.MEETINGS, meetingListView);
				} else if(tabId.equals(TAB_ID[2])==true){
					setBoardListData(mContext, ApiCode.ADVERTISEMENTS, advertisementListView);
				}
			}
			
		});
		
		setBoardListData(this, ApiCode.NOTICES, noticeListView);
	}

	private void setBoardListData(final Context mContext, final String code, final ListView listview) {
		ThreadInterface threadInterface = new ThreadInterface() {
			BoardList boardList;

			public void onThread() {
				// TODO Auto-generated method stub
				String api = null;
				if (code.equals(ApiCode.OCCASIONS) == true) {
					api = ApiCode.API_OCCASION;
				} else if (code.equals(ApiCode.MEETINGS) == true) {
					api = ApiCode.API_MEETING;
				} else if (code.equals(ApiCode.ADVERTISEMENTS) == true) {
					api = ApiCode.API_ADVERTISEMENT;
				} else if (code.equals(ApiCode.NOTICES) == true) {
					api = ApiCode.API_NOTICE;
				}
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, api,	"GET", "");
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub

				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					if (boardList.getItemCount() > 0) {
						ListAdapter listAdapter = null;
						if (code.equals(ApiCode.OCCASIONS) == true
								|| code.equals(ApiCode.MEETINGS) == true
								|| code.equals(ApiCode.ADVERTISEMENTS) == true) {
							listAdapter = new ThreeTextBoardListAdapter(mContext, R.layout.listitem_threetext, (ArrayList<BoardListItem>) boardList.getList());

						} else if (code.equals(ApiCode.NOTICES) == true) {
							listAdapter = new TwoTextListAdapter(mContext, R.layout.listitem_twotext, (ArrayList<BoardListItem>) boardList.getList());
						}
						listview.setAdapter(listAdapter);

						listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							public void onItemClick(AdapterView<?> parent,
									View arg1, int position, long id) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(mContext, BoardDetailActivity.class);
								Integer wr_id = boardList.getList().get(position).getWr_id();
								//intent.putExtra("userid", ApiCode.USERID);
								intent.putExtra("wr_id", wr_id);
								intent.putExtra("boardname", code);
								startActivity(intent);
							}
						});

					} 
				}
				else {
					requestGson.alertToast(mContext, code+" 데이터 로딩 실패");
				}

			}
		};

		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}

	private void setMemberListData(final Context mContext, final String code, final ListView listview) {
		ThreadInterface threadInterface = new ThreadInterface() {
			MemberList memberList;
			public void onThread() {
				// TODO Auto-generated method stub
				String api = ApiCode.API_RECENTUSER;
				
				memberList = requestGson.getMemberGsonBySendingAPI(mContext, api, "GET", "");
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub

				if (memberList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					if (memberList.getItemCount() > 0) {
						ListAdapter listAdapter = null;
						listAdapter = new ThreeTextMemberListAdapter(mContext, R.layout.listitem_threetext,	(ArrayList<MemberListItem>)memberList.getList());
						listview.setAdapter(listAdapter);
						
						listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
							public void onItemClick(AdapterView<?> parent, View arg1,
									int position, long id) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(mContext, MemberDetailActivity.class);
								String favoriteid = memberList.getList().get(position).getUserid();

								//intent.putExtra("userid", ApiCode.USERID);
								intent.putExtra("favoriteid", favoriteid);
								startActivity(intent);
							}
						});

					} 
				}
				else {
					requestGson.alertToast(mContext, code+" 데이터 로딩 실패");
				}

			}
		};

		ThreadTask task = new ThreadTask((Activity) mContext, threadInterface);
		task.execute();
	}
}
